<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-menu.tld" prefix="menu" %>
<%@ taglib uri="/WEB-INF/tlds/struts-menu-el.tld" prefix="menu-el" %>
<%@ taglib uri="/WEB-INF/tlds/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tlds/fmt.tld" prefix="fmt" %>
<%@ taglib uri="/WEB-INF/tlds/authz.tld" prefix="authz" %>
<%@ taglib uri="/WEB-INF/tlds/sitemesh-decorator.tld" prefix="decorator" %>
<c:set var="ctx" value="${pageContext.request.contextPath}" scope="request"/>
<script type="text/javascript" src="${ctx}/dwr/interface/ConfigSaverDWR.js"></script>
<script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
<script type="text/javascript" src="${ctx}/dwr/util.js"></script>
<script type="text/javascript">
<!--
	function modifyVar() {
		var varVal=checkValue($('varForm'),'varId');
		if(!varVal || varVal.length==0) {
			alert('<bean:message key="info.selectVariable" />');
		} else if(confirm('<bean:message key="info.confirmVarModification" />')) {
			popup('variable.do?method=edit&decorator=popup&confirm=true&id='+varVal, 'popupVar', 400, 280);
		}
	}
	function addVar() {
		popup('variable.do?method=edit&decorator=popup&confirm=true', 'popupVar', 400, 280);
	}
	function deleteVar() {
		var varVal=checkValue($('varForm'),'varId');
		if(!varVal || varVal.length==0) {
			alert('<bean:message key="info.selectVariable" />');
		} else if(confirm('<bean:message key="info.confirmVarDeletion" />')) {
			$('methodVar').value='delete';
			$('varForm').submit();
		}
	}
	function saveConfig() {
		// AJAX call
		var id = $F("id");
		ConfigSaverDWR.saveApplicationConfig(message,id);
	}					
	function message(ret) {
		if(ret) {
			alert('<bean:message key="info.saveDone"/>');
			this.location = '${ctx}/variable.do?method=list';
		} else {
			alert('<bean:message key="info.saveNotDone"/>');
		}
	}
//  -->
</script>
<div id="tabPanelWrapper">
	<div id="tabPanel">
		<ul>
			<li><a href="application.do?method=edit" onclick=""><bean:message key="application.generalTitle" /></a></li>
			<li><a href="application.do?method=list" onclick=""><bean:message key="application.resourceTitle" /></a></li>
			<li><a id="currentTab" href="#" onclick=""><bean:message key="application.variableTitle" /></a></li>
		</ul>
	</div>
	<div id="tabContent">
		<html:form styleId="varForm" action="/variable">	
			<h1><bean:write name="currentApplication" property="name" /></h1>
			<div id="subcontent">
				<input type="hidden" id="methodVar" name="method" value="" />
				<html:hidden styleId="id" property="application.id"/>
				<p><bean:message key="variablesPage.variableList" /></p>
				<table class="list" id="varList">
					<thead>
						<tr>
				<authz:authorize ifAllGranted="ROLE_SUPERVISOR">								
							<th>-</th>
				</authz:authorize>
							<th><bean:message key="variablesPage.name" /></th>
							<th><bean:message key="variablesPage.value" /></th>
						</tr>
					</thead>
					<tbody>
						<c:forEach var="variable" items="${currentApplication.variables}">
							<tr>
				<authz:authorize ifAllGranted="ROLE_SUPERVISOR">								
								<td><input type="radio" name="varId" value="${variable.id}" /></td>
				</authz:authorize>
								<td><c:out value="${variable.name}" /></td>
								<td><c:out value="${variable.value}" /></td>
							</tr>
						</c:forEach>
					</tbody>
				</table>
				<authz:authorize ifAllGranted="ROLE_SUPERVISOR">								
					<p>	
					<input name="addVarButton" type="submit" value="<bean:message key='variablesPage.add' />" onclick="addVar();return false;" />&nbsp;
					<input name="modifyVarButton" type="submit" value="<bean:message key='variablesPage.modify' />" onclick="modifyVar();return false;" />&nbsp;
					<input name="deleteVarButton" type="submit"	value="<bean:message key='variablesPage.delete' />" onclick="deleteVar();return false;" />
					</p>
				</authz:authorize>
			</div>
		</html:form>
		<p>
		<authz:authorize ifAllGranted="ROLE_SUPERVISOR">								
			<input type="submit" value="<bean:message key='allPage.saveConfig' />" onclick="saveConfig();return false;"/>
		</authz:authorize>
		</p>
	</div>
</div>